﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.DAL;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class frmExportBefore : Form
    {


        OrderBefore orderBefore = new OrderBefore(); 
        List<Order_Detail> lstOrder = new List<Order_Detail>();

        public frmExportBefore()
        {
            InitializeComponent();
        }
        

        private void frmExportBefore_Load(object sender, EventArgs e)
        {
            txtOrderID.Text = new Random().Next(1, 100).ToString();
            DataCSQLDataContext datacontext = new DataCSQLDataContext(new Controler().ConnectionString);
            try
            {
                IEnumerable<Product> lstProduct = from d in datacontext.Products
                                                  select d;
                MessageBox.Show(lstProduct.Count().ToString());
                foreach (Product p in lstProduct) cmbProductID.Items.Add(p);
                cmbProductID.DisplayMember = "ProductID";
                cmbProductID.ValueMember = "ProductID";
                cmbProductID.SelectedIndex = 0;
            }
            catch
            {
                //khong co du lieu
            }
            try
            {
                cmbCustomerID.DataSource = from d in datacontext.Customers
                                           select d;
                cmbCustomerID.DisplayMember = "CustomerID";
            }
            catch
            {
                //khong co du lieu
            }
            try
            {
                //cmbUnitMoney.DataSource = from d in datacontext.UnitMoneys
                //                          select d;
                //cmbUnitMoney.DisplayMember = "Id";
            }
            catch
            {
                //khong co lieu
            }
        }


        //Load data to all control
        private void LoadDataToAllControl()
        {
            txtOrderID.Enabled = false;
            this.dtpDateOrder.Value = Controler.timeOfProgram();
            this.dtpDateShip.Value = Controler.timeOfProgram();


        }

        private void btnProductAdd_Click(object sender, EventArgs e)
        {
            if (cmbProductID.Items.Count == 0) MessageBox.Show("Không có sản phẩm nào cả!");
            else if (cmbProductID.SelectedIndex == -1) MessageBox.Show("Chưa chọn sản phẩm!");
            else if (string.IsNullOrWhiteSpace(txtProductNumber.Text)) MessageBox.Show("Chưa nhập số lượng!");
                else
                {
                    Product productSelected = (Product)cmbProductID.SelectedItem;
                    Order_Detail orderDetail = new Order_Detail();
                    orderDetail.OrderID = txtOrderID.Text;
                    orderDetail.ProductID = cmbProductID.Text;
                    orderDetail.Quantity = Convert.ToInt16(txtProductNumber.Text);
                    orderDetail.UnitPrice = productSelected.Price.Value;
                    lstOrder.Add(orderDetail);

                    decimal totalMoney = productSelected.Price.Value * Convert.ToInt32(txtProductNumber.Text) * (1 + (decimal)productSelected.RateVAT.Value / 100);
                    dgvOrderInfomation.Rows.Add(productSelected.ProductName, txtProductNumber.Text, productSelected.Price.ToString(), productSelected.RateVAT.Value, totalMoney);

                    //remover from combobox
                    cmbProductID.Items.Remove(cmbProductID.SelectedItem);
                    cmbProductID.Text = "";
                }
        }

        private void btnExportOrder_Click(object sender, EventArgs e)
        {
            try
            {

                if (this.TestDataInput().Count==0)
                {
                    InsertDatabase();
                    MessageBox.Show("Đã thêm hóa đơn xong!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi" + ex.Message);
            }
        }


        private List<string> TestDataInput()
        {
            List<string> _lstError = new List<string>();
            if (string.IsNullOrEmpty(this.txtOrderID.Text)) _lstError.Add("Chưa có mã hóa đơn!");
            if ((this.dtpDateShip.Value - this.dtpDateOrder.Value).TotalMinutes < 0) _lstError.Add("Ngày giao hàng nhỏ hơn ngày lập hóa đơn!");
            if (this.cmbCustomerID.SelectedIndex == -1) _lstError.Add("Chưa chọn người mua hàng!");
            if (dgvOrderInfomation.Rows.Count == 0) _lstError.Add("Chưa có mặt hàng nào cả!");
            return _lstError;     
        }


        private void InsertDatabase ()
        {
           
                //đưa dữ liệu vào database
                DataCSQLDataContext dataContext = new DataCSQLDataContext(new Controler().ConnectionString); //creat a before

            //create a Order Beforer 
                OrderBefore newOrder = new OrderBefore();
                newOrder.OrderBeforeId = txtOrderID.Text;
                newOrder.CustomerId = ((Customer)cmbCustomerID.SelectedValue).CustomerID;
                newOrder.EmployId = ((Employee)cmbEmployee.SelectedValue).EmployeeID.ToString();
                newOrder.MoneyDeposit = Convert.ToDecimal(txtMoneyDeposit.Text);
                newOrder.OrderBeforeDate = dtpDateOrder.Value;
                newOrder.RequirementDate = dtpDateShip.Value;
                dataContext.OrderBefores.InsertOnSubmit(newOrder);

            //create Order detail 
                foreach (Order_Detail o in lstOrder)
                {
                    dataContext.Order_Details.InsertOnSubmit(o); 
                }

                try
                {
                    dataContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi supmit:" + ex.Message); 
                }

        }
        


     
        private void cmbProductID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Product productSelected = (Product)cmbProductID.SelectedItem;
            txtProductName.Text = productSelected.ProductName.ToString();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbCustomerID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Customer customerSelected = (Customer)cmbCustomerID.SelectedItem;
            txtCustomerName.Text = customerSelected.ContactName.ToString();
        }
    }
}
